home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacPeople 2003 February 1
/
MACPEOPLE-2003-02-01.ISO.7z
/
MACPEOPLE-2003-02-01.ISO
/
ぶらりオンラインウェアの旅
/
おしゃべり漂流記
/
xGates
/
xGates 1.2 Source Code.sit
/
xGates 1.2 Source Code
/
prefs.c
< prev
next >
Wrap
Text File
|
2002-12-08
|
5KB
|
203 lines
/*
xGates -- Stunningly entertaining action game for MacOS Classic / MacOS X
Copyright (C) 2002 Sveinbjorn Thordarson <paladeen@soth.zoneit.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
prefs.c
*/
#include "externs.h"
#include "prototypes.h"
#include "definitions.h"
////////////////////////////////////////////////
//Locate Preferences file and load it, regardless
////////////////////////////////////////////////
void LoadPrefs (void)
{
OSErr myErr = noErr;
FSSpec file;
short refNum;
short prefRefNum;
long prefDirID;
//locate preferences folder
myErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, false, &prefRefNum, &prefDirID);
if (myErr)
{
DoAlert("¥pError reading preferences.", "¥pCould not locate Preferences folder.");
QuitApp();
}
//create the file pref spec to open
FSMakeFSSpec(prefRefNum, prefDirID, kPreferencesFileName, &file);
//try to open it
myErr = FSpOpenDF(&file, fsRdPerm, &refNum);
FSClose(refNum);
//if open fails, create the default prefs file
if (myErr)
{
CreatePrefs(file);
}
//otherwise...read 'em
else
{
//load spec into the prefs global structure
prefsFile = file;
//read
ReadPrefs(prefsFile);
}
}
////////////////////////////////////////////////
//Read prefs file into prefsStruct structure
////////////////////////////////////////////////
void ReadPrefs (FSSpec fileSpec)
{
OSErr myErr = noErr;
long count;
short refNum;
myErr = FSpOpenDF(&fileSpec, fsRdPerm, &refNum);
if (myErr)
{
DoAlert("¥pError opening preferences.", "¥pTry deleting the 'xGates Preferences' file in Preferences folder.");
QuitApp();
}
count = sizeof(short);
FSRead(refNum, &count, &prefs.appVersionMajor);
FSRead(refNum, &count, &prefs.appVersionMinor);
//if it's a prefs file for an older/newer version
if ( prefs.appVersionMajor != kAppVersionMajor ||
prefs.appVersionMinor != kAppVersionMinor )
{
FSClose(refNum);
CreatePrefs(fileSpec);
return;
}
//read the data from file into fields in prefs structure
count = sizeof(HighScoreStruct);
FSRead(refNum, &count, &prefs.highscores);
count = sizeof(short);
FSRead(refNum, &count, &prefs.music);
FSRead(refNum, &count, &prefs.sound);
FSRead(refNum, &count, &prefs.lastHighScore);
FSRead(refNum, &count, &prefs.noChainsawJamming);
FSRead(refNum, &count, &prefs.fixedFrameRate);
FSRead(refNum, &count, &prefs.drawFPS);
FSClose(refNum);
}
////////////////////////////////////////////////
//Generate new prefs file with defaults
////////////////////////////////////////////////
void CreatePrefs (FSSpec fileSpec)
{
OSErr myErr = noErr;
//set the defaults
prefs.appVersionMajor = kAppVersionMajor;
prefs.appVersionMinor = kAppVersionMinor;
ClearHighscoreList();//equivalent to creating a new one
prefs.music = true;
prefs.sound = true;
prefs.lastHighScore = kNoHighScoreSet;
prefs.noChainsawJamming = false;
prefs.fixedFrameRate = true;
prefs.drawFPS = false;
//copy file spec into global prefs structure
prefsFile = fileSpec;
//create it on disk
myErr = FSpCreate(&prefsFile, 'xGat', 'pref', smSystemScript);
if (myErr == -48) //file already exists, then delete it and try again
{
HDelete(prefsFile.vRefNum, prefsFile.parID, prefsFile.name);
myErr = FSpCreate(&prefsFile, 'xGat', 'pref', smSystemScript);
}
//then write the defaults to it
if(myErr || WritePrefs())
{
DoAlert("¥pError creating/writing preferences.", "¥pTry deleting the 'xGates Preferences' file in the Preferences folder.");
QuitApp();
}
}
////////////////////////////////////////////////
//Write prefsStruct structure to file
////////////////////////////////////////////////
short WritePrefs (void)
{
OSErr myErr = noErr;
short refNum;
long count;
//open with file spec from prefs
myErr = FSpOpenDF(&prefsFile, fsRdWrPerm, &refNum);
if (myErr)
{
DoAlert("¥pError writing preferences.", "¥pTry deleting the 'xGates Preferences' file in Preferences folder.");
QuitApp();
}
//write the prefs
count = sizeof(short);
FSWrite(refNum, &count, &prefs.appVersionMajor);
FSWrite(refNum, &count, &prefs.appVersionMinor);
count = sizeof(HighScoreStruct);
FSWrite(refNum, &count, &prefs.highscores);
count = sizeof(short);
FSWrite(refNum, &count, &prefs.music);
FSWrite(refNum, &count, &prefs.sound);
FSWrite(refNum, &count, &prefs.lastHighScore);
FSWrite(refNum, &count, &prefs.noChainsawJamming);
FSWrite(refNum, &count, &prefs.fixedFrameRate);
FSWrite(refNum, &count, &prefs.drawFPS);
FSClose(refNum);
return(myErr);
}